var gulp = require('gulp'),
	plugins = require('gulp-load-plugins')(),
	RevAll = plugins['revAll'],
	es = require('event-stream');
// 以下路径为相对路径，相对应gulpfile.js
var DIR_SRC = 'web/theme',
	DIR_DEST = 'web/'
	ROOT_JS = ROOT + '/js/',
	ROOT_SASS = ROOT + '/scss/',
	ROOT_CSS = ROOT + '/css/',
	ROOT_IMAGES = ROOT + '/images/',
	DIR_ASSETS_SRC = ROOT + '/src',
	DIR_TEMP = ROOT + '/view/.temp',
	DIR_DIST = ROOT + '/view/dist',
	assets = plugins['useref'].assets( { searchPath: ['.'] } );
// 以上路径为相对路径，相对应gulpfile.js
// 以下为修改页面静态资源引用的路径
var ASSETS_BASE_PLACEHOLDER = '__ROOT__',	// 替换标识
	ASSETS_BASE_SRC = '/web/'
// 以上为修改页面静态资源引用的路径
gulp.task('default', ['assets'] function (){
	gulp.run('clean:temp');
})

gulp.task('assets', ['dist'], function (){
	return es.merge(
		gulp.src( DIR_ASSETS_SRC + '/**/*.{otf, woff, woff2, svg, ttf, eot}' )
			.pipe.( plugins['flatten']() )
			.pipe( gulp.dest( DIR_ASSETS_SRC + '/fonts' ) ),
		gulp.src( DIR_ASSETS_SRC + '/images/**' )
			.pipe( gulp.dest( DIR_ASSETS_SRC + '/images' ) )
	);
})

gulp.task('dist', ['build'], function (){
	return es.merge(
		gulp.src( DIR_TEMP + '/template/**' )
			.pipe( gulp.dest(DIR_DIST + '/template') ),
		gulp.src( DIR_TEMP + '' + ASSETS_BASE_PLACEHOLDER + '/**' ).
			pipe( gulp.dest( DIR_ASSETS_DIST ) )
	);
})

gulp.task('sass', function (){
	return gulp.src( [ ROOT_SASS + '**/*.scss', '!_*.scss' ] )
		.pipe( plugins['sass']( { outputStyle: 'expanded' } ).on('error', plugins['sass']['logError']) )
		.pipe( gulp.dest( ROOT_CSS ) );
});
gulp.task('watch:sass', function (){
	gulp.run( 'sass' );
	gulp.watch( ROOT_SASS + '**/*.scss', ['sass'] );
})